package com.mxpio.erp.inventory.service.impl;

import com.mxpio.erp.common.inventory.entity.VItemConsumedByDeptPerDay;
import com.mxpio.erp.common.inventory.service.VItemConsumedByDeptPerDayService;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.lin.Linq;
import com.mxpioframework.jpa.query.Criteria;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author : 吴盼盼
 * @Datetime : 2022/10/8 21:27
 * @Desc :  统计部门日度消耗
 * @Modor :  Modifytime:
 * @modDesc :
 */
@Service
public class VItemConsumedByDeptPerDayServiceImpl implements VItemConsumedByDeptPerDayService {
    @Override
    @Transactional(readOnly = true)
    public List<VItemConsumedByDeptPerDay> list(Criteria c) {
        Linq linq =JpaUtil.linq(VItemConsumedByDeptPerDay.class);
        // @formatter:off
        return linq.select("itemCode","createDept","itemName","itemGroupCode","itemSpec","unitCode",linq.criteriaBuilder().sum(linq.root().get("quantity")).alias("quantity"),linq.criteriaBuilder().sum(linq.root().get("fee")).alias("fee"))
                .where(c)
                .groupBy("itemCode","createDept","itemName","itemGroupCode","itemSpec","unitCode")
                .list();
    }

    @Override
    @Transactional(readOnly = true)
    public Page<VItemConsumedByDeptPerDay> listPage(Pageable pageAble, Criteria c) {
        Linq linq =JpaUtil.linq(VItemConsumedByDeptPerDay.class);
        // @formatter:off
        return linq.select("itemCode","createDept","itemName","itemGroupCode","itemSpec","unitCode",linq.criteriaBuilder().sum(linq.root().get("quantity")).alias("quantity"),linq.criteriaBuilder().sum(linq.root().get("fee")).alias("fee"))
                .where(c)
                .groupBy("itemCode","createDept","itemName","itemGroupCode","itemSpec","unitCode")
                .paging(pageAble);
    }

    @Override
    @Transactional(readOnly = true)
    public VItemConsumedByDeptPerDay listPageSumFee(Criteria c) {//根据条件汇总fee
        Linq linq =JpaUtil.linq(VItemConsumedByDeptPerDay.class);
        return linq.select(linq.criteriaBuilder().sum(linq.root().get("fee")).alias("fee"))
                .where(c)
                .findOne();
    }
}
